
import
	tango.io.device.Array,
	tango.stdc.stringz,
	tango.io.Stdout ,
	
	tango.math.random.Random,
	tango.net.device.Berkeley,
	tango.net.InternetAddress,
	tango.net.InternetAddress,
	Int	= tango.text.convert.Integer ,
	tango.core.Array ,
	tango.io.device.Array ,
	tango.io.device.File ,
	libeventd.Types ;


const NEVENT	= 6000 ;
event*[NEVENT]	ev ;
int	called;
extern(C)  void time_cb(evutil_socket_t fd, short event, void *arg){
	timeval tv;
	int i, j;
	called++;
	if (called < 10*NEVENT ) {
		for (i = 0; i < 10; i++) {
			j = rand_int(NEVENT);
			tv.tv_sec = 0;
			tv.tv_usec = rand_int(15000);
			if (tv.tv_usec % 2){
				evtimer_add(ev[j], &tv);
			}else{
				evtimer_del(ev[j]) ;
			}
		}
	}
}

int rand_int(int to){
	auto r	= new RandomG!()() ;
	auto r2	= r.uniformRBoundsD!(int)(to);
	return r2.getRandom ;
}

void test_time() {
	
	auto eb	= event_init();
	Stdout.formatln("{}", eb).flush ;
	timeval tv;
	
	auto r	= new RandomG!()() ;
	auto r2	= r.uniformRBoundsD!(int)(15000);
	
	for (int i = 0; i < NEVENT; i++) {
		ev[i] = new event;
		evtimer_set(ev[i], &time_cb, ev[i]);
		tv.tv_sec = 0;
		tv.tv_usec = r2.getRandom ;
		evtimer_add(ev[i], &tv);
	}
	event_dispatch();
	
	assert( called >= NEVENT);
}

void main(){
	WSADATA wd = void;
	WSAStartup (0x0202, &wd);
	
	test_time;
}
